# application/api/detection/product_model.py
from flask import Blueprint, jsonify, request
from application.services.detection.product_model import ProductModelService
from application.utils.res import json_fail, json_success, page_success

# 创建蓝图
bp = Blueprint("product_model", __name__, url_prefix="/product/model")


@bp.get("/list")
def get_product_model_list():
    """
    分页获取产品型号列表
    支持条件查询：pageNum, pageSize, productModel, productType, productPhase
    """
    try:
        # 获取分页参数
        page_num = int(request.args.get("pageNum", 1))
        page_size = int(request.args.get("pageSize", 10))

        # 获取查询条件
        conditions = {
            "productModel": request.args.get("productModel"),
            "productType": request.args.get("productType"),
        }

        # 过滤空值条件
        filtered_conditions = {
            k: v for k, v in conditions.items() if v is not None and v != ""
        }

        # 调用服务获取数据
        product_list, total = ProductModelService.get_product_model_list(
            page_num, page_size, filtered_conditions
        )

        return page_success(product_list, total)
    except Exception as e:
        return json_fail(f"查询失败: {str(e)}"), 500

@bp.post("")
def create_product_model():
    """
    创建产品型号
    """
    try:
        # 获取请求数据
        data = request.get_json()
        
        if not data:
            return json_fail("请求数据不能为空"), 400
            
        result = ProductModelService.create_product_model(data)
        return json_success(result, "创建成功", 200)
    except Exception as e:
        return json_fail(f"创建失败: {str(e)}"), 500


@bp.put("")
def update_product_model():
    """
    更新产品型号
    :param product_model_id: 产品型号ID
    """
    try:
        # 获取请求数据
        data = request.get_json()
        
        if not data:
            return json_fail("请求数据不能为空"), 400
        
        product_model_id = data.get("productModelId")
        if not product_model_id:
            return json_fail("产品型号ID不能为空"), 400
        
        # 正确调用服务层方法 - 传递两个参数
        result = ProductModelService.update_product_model(product_model_id, data)
        if result:
            return json_success(result, "更新成功")
        else:
            return json_fail("产品型号不存在"), 404
    except Exception as e:
        return json_fail(f"更新失败: {str(e)}"), 500


@bp.delete("/<int:product_model_id>")
def delete_product_model(product_model_id):
    """
    删除产品型号
    :param product_model_id: 产品型号ID
    """
    try:
        result = ProductModelService.delete_product_model(product_model_id)
        if result:
            return json_success(None, "删除成功")
        else:
            return json_fail("产品型号不存在"), 404
    except Exception as e:
        return json_fail(f"删除失败: {str(e)}"), 500
    


@bp.get("/optionselect")
def get_product_model_optionselect():
    """
    获取产品型号下拉列表
    """
    try:
        result = ProductModelService.get_product_model_optionselect()
        return json_success(result)
    except Exception as e:
        return json_fail(f"查询失败: {str(e)}"), 500
    